var totalPage;
var totalCnt;
var weChatIDListData = [];

$(document).ready(function() {
	setBaseUrl();
	$('title', parent.document).html($('title').text());
	createTableHead("userListThead", userListTable, search);
	search();
	initPage();
})

function search(){
	$ajax('searchWechatUser', 'searchUserList', searchUserList);
}
function searchUserList(data){
    setPage(data.total, $("#rows").val());
	weChatIDListData = data && data.userlist || [];
	if (weChatIDListData == "Error") {
	    weChatIDListData = [];
	}
	createTableBody("userListTbody", userListTable, weChatIDListData);
	if (data && data.result == "Pulled") {
        showMessage(messageContent["Pull.Pulled"]);
        setTimeout(hideMessge, 3000);
    }
}
function chkBefore(index, chkFlag){
	var checkbox =document.getElementsByName("chk"); 
	for (var i=0; i<checkbox.length; i++) {
		checkbox[i].checked = false; 
	}
	checkbox[index].checked = true; 
	if (chkFlag == "update") {
		update(index);
	} else if(chkFlag == "delete") {
		del();
	} else if(chkFlag == "sync") {
		sync();
	}
}
function sync(){
	var checkbox = getCheckbox("chk");
	if (checkbox.count == 0) {
		showMessage(messageContent["Select.NotFound"]);
	} else {
		showMessage(messageContent["Sync.Confirm"], "model2");
		flag = "Sync";
	}
}
function getUrl(){
	if(flag == "Update"){
		return 'updateWechatUser';
	} else if(flag == "Save"){
        return 'saveWechatUser';
	} else if(flag == "Delete"){
		return 'deleteWechatUser';
	} else if(flag == "Sync"){
		return 'syncWechatUser';
	} else if(flag == "Pull"){
		return 'pullSubscribeUser';
	}
}
function getData(){
	return {weChatIDList: getCheckDataList(weChatIDListData, "weChatID")};
}
function update(index) {
	$('#title').html('');
	flag = "Update";
	addTitle("wechatUserUpdate");
	allTable(index);
}
function save() {
	flag = "Save";
	addTitle("wechatUserSave");
	allTable();
}
function allTable(index){
	createComBody("detailOneBody", userListTable, weChatIDListData[index], 1);
    if (flag == "Save") {

    } else if (flag == "Update") {
        $("#weChatID").attr("readonly", "readonly");
        $("#weChatID").val(weChatIDListData[index].weChatID);
        if (weChatIDListData[index].loginEnabled == false) {
            $("#loginEnabled").val(0);
        } else {
            $("#loginEnabled").val(1);
        }
    }
	$("#save").modal("show");
}
function confirm(){
	return checkForm("saveForm", userListTable);
}
function saveWechatUser(data){
     var result = data && data.result;
     if (result == "Success"){
    	 showMessage(messageContent[flag + ".Success"]);
    	 $('#save').modal('hide');
    	 formReset("saveForm");
     } else if (result == "NotFound"){
    	 showMessage(messageContent[flag + ".Failed"]);
     } else {
         showMessage(messageContent[flag + ".Error"]);
     }
     search();
}
function pullSubscribeUser(){
    flag = "Pull";
	showMessage(messageContent["Pull.Confirm"], "model2");
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessage(messageContent[flag + ".Success"]);
	} else if (result == "Pulling") {
	    showMessage(messageContent[flag + ".Pulling"]);
	    setTimeout(hideMessge, 3000);
	} else if (result == "Pulled") {
	    showMessage(messageContent[flag + ".Pulled"]);
	    setTimeout(hideMessge, 3000);
	} else if (result == "NotFound") {
	    var message = messageContent[flag + ".NotFound"];
	    if (data && data.notFoundList) {
            message += "<br/>" + data.notFoundList;
        }
		showMessage(message);
	} else {
		showMessage(messageContent[flag + ".Error"]);
	}
    search();
}
